package com.example.administrator.szb.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;

import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BaseActivity;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.util.DialogUtil;
import com.example.administrator.szb.util.QTLog;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by D N on 2017/12/6.
 */
@SuppressLint("SetJavaScriptEnabled")
public class BannerActivity extends BaseActivity {
    @Bind(R.id.banner_webview)
    WebView textWeb;

    @SuppressLint("JavascriptInterface")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.banner_activity);
        ButterKnife.bind(this);
        SampleApplicationLike.getActivitiesInstance().add(this);
        initToolbar(R.id.banner_toolbar_include,getIntent().getStringExtra("title"));
        textWeb.getSettings().setJavaScriptEnabled(true);
        textWeb.getSettings().setDomStorageEnabled(true);
//    添加js对象(必要)
        textWeb.addJavascriptInterface(new JavaScriptInterfaceZX(this), "registerActionAndroid");
//        textWeb.setWebChromeClient(new WebChromeClient());
//        textWeb.setWebViewClient(new WebViewClient() {
//            @Override
//            public void onPageFinished(WebView view, String url) {
//                super.onPageFinished(view, url);
//                testControl();
//            }
//        });
        textWeb.loadUrl(getIntent().getStringExtra("url"));
        //textWeb.loadUrl("file:///android_asset/test.html");
    }

    private class JavaScriptInterfaceZX{
        Activity activity;

        public JavaScriptInterfaceZX(Activity activity) {
            this.activity = activity;
        }

        @JavascriptInterface
        public void postMessage(Object o) {
            QTLog.e("postMessage: +++++++han to");
            //Toasts.show(activity,"test", Toast.LENGTH_SHORT);
            if (SampleApplicationLike.getUserloginInstance().getId() == 0){
                startActivity(new Intent(activity,RegisterActivity_info.class));
            }else {
                DialogUtil.showToast(activity,"您已经在登录状态中");
            }

        }
    }
}
